<template>
  <div class="login">
    <h2>Test文库</h2>
    <el-form>
      <el-input placeholder="用户名" v-model="user.name" />
      <el-input
        type="password"
        placeholder="密码"
        show-password
        v-model="user.pass1"
      />
      <el-button class="btn" @click="subfun">登 录</el-button>
    </el-form>
    <p><router-link to="/res">快速注册</router-link><a href="#">忘记密码</a></p>
  </div>
</template>

<script>
import store from "../store";
export default {
  name: "Login",
  data() {
    return {
      user: { name: "", email: "", pass1: "", pass2: "" },
      msg: "",
    };
  },
  methods: {
    subfun: function () {
      if (this.user.name == "") {
        this.msg = "用户名不能为空";
      } else if (this.user.pass1 == "") {
        this.msg = "密码不能为空";
      } else {
        var data = store.state.userdata;
        var flg = false;
        for (var i = 0; i < data.length; i++) {
          if (
            data[i].name == this.user.name &&
            data[i].pass1 == this.user.pass1
          ) {
            flg = true;
          }
        }
        if (flg) {
          this.$router.push({ path: "/" });
        } else {
          this.msg = "用户名或密码不对";
        }
      }
    },
  },
};
</script>

<style scoped></style>
